package com.wg.controller;

import com.wg.R;
import com.wg.entity.Users;
import com.wg.entity.dto.UserLoginDto;
import com.wg.entity.dto.UserRegisterDto;
import com.wg.entity.vo.MyScoreVo;
import com.wg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * web端的注册功能
     */
    @PostMapping("/register")
    public R register(@RequestBody @Validated UserRegisterDto user) {
        return userService.register(user);
    }

    /**
     * web端的登录功能
     */
    @PostMapping("/loginIn")
    public R<Users> login(@RequestBody @Validated UserLoginDto user, HttpServletRequest request) {
        return userService.login(user, request);
    }

    @GetMapping("/loginOut")
    public R logout(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        return R.ok();
    }

    /**
     * 获取积分排名
     */

    @GetMapping("/getScore/{id}")
    public R<MyScoreVo> getMyScore(@PathVariable Long id) {
        return userService.getMyScore(id);
    }

    @GetMapping("/getAllScore")
    public R<List<MyScoreVo>> getMyScore() {
        return userService.getAllScore();
    }

}
